package com.querie.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.querie.model.beans.FragmentSession;
import com.querie.sql.BaseDAO;
import com.querie.sql.PreparedStatementSetter;
import com.querie.sql.ResultSetExtractor;

public  class FragmentsessionDAOImpl extends BaseDAO implements FragmentsessionDAO{
	public FragmentSession load(final long sessionid, final long fragmentid) throws Exception{
		String sql = "SELECT * FROM FragmentSession WHERE sessionid=? and fragmentid=?";
		return (FragmentSession) getRow(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setLong(1, sessionid);
				ps.setLong(2, fragmentid);
			}
		}, new ResultSetExtractor() {
			public Object extract(ResultSet rs) throws SQLException {
			FragmentSession row  = new FragmentSession();
				row.setSessionid(rs.getLong("sessionid"));
				row.setImportance(rs.getDouble("importance"));
				row.setFragmentid(rs.getLong("fragmentid"));
				return row;
			}
	});
	}

	public int delete(final long sessionid, final long fragmentid) throws Exception{
		String sql = "DELETE FROM FragmentSession WHERE sessionid=? and fragmentid=?";
		return executeUpdate(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setLong(1, sessionid);
				ps.setLong(2, fragmentid);
			}
		});
	}

	public int update(final FragmentSession data) throws Exception{
		String sql = "UPDATE FragmentSession SET importance = ? WHERE sessionid=? and fragmentid=?";
		return executeUpdate(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setDouble(1, data.getImportance());
				ps.setLong(2, data.getSessionid());
				ps.setLong(3, data.getFragmentid());
				
			}
	});
	}

	public long insert(final FragmentSession data) throws Exception{
		String sql = "INSERT INTO FragmentSession ( importance, fragmentid, sessionid) VALUES (?, ?, ? )";
		long id = executeInsert(sql, new PreparedStatementSetter() {
			public void prepare(PreparedStatement ps) throws SQLException {
				ps.setDouble(1, data.getImportance());
				ps.setLong(2, data.getFragmentid());
				ps.setLong(3, data.getSessionid());
			}
		});
		return id;
	}

	public List<FragmentSession> queryAll()throws Exception{
		String sql = "SELECT * FROM FragmentSession";
		return (List) getList(sql, new ResultSetExtractor() {
			public Object extract(ResultSet rs) throws SQLException {
				FragmentSession row = new FragmentSession();
				row.setSessionid(rs.getLong("sessionid"));
				row.setImportance(rs.getDouble("importance"));
				row.setFragmentid(rs.getLong("fragmentid"));
				return row;
			}
	});
	}

	public List<FragmentSession> queryAllWithPage(int startOfPage, int...pageSize)throws Exception{
		
		int page = 20;
		if (pageSize.length > 0)
		{
			page = pageSize[0];
		}
		String sql = "SELECT * FROM FragmentSession LIMIT "+startOfPage+" ,"+page ;
		@SuppressWarnings("unchecked")
		List<FragmentSession> list = ((List<FragmentSession>) getList(sql, new ResultSetExtractor() {
			public Object extract(final ResultSet rs) throws SQLException {
				FragmentSession row = new FragmentSession();
				row.setSessionid(rs.getLong("sessionid"));
				row.setImportance(rs.getDouble("importance"));
				row.setFragmentid(rs.getLong("fragmentid"));
				return row;
			}
	}));
		return list;
	}

	 public ResultSetExtractor getListResultSetExtractor(){
		return new ResultSetExtractor() {
			public Object extract(ResultSet rs) throws SQLException {
				FragmentSession row = new FragmentSession();
				row.setSessionid(rs.getLong("sessionid"));
				row.setImportance(rs.getDouble("importance"));
				row.setFragmentid(rs.getLong("fragmentid"));
				return row;
			}
	};
	}

	}